local yaolie = fk.CreateSkill {
  name = "lvshi__yaolie",
}

Fk:loadTranslationTable{
  ["lvshi__yaolie"] = "邀烈",
  [":lvshi__yaolie"] = "轮次开始时，你可指定一名未明置身份的角色，其于本轮明置身份时，你令其视为使用你选择目标的【决斗】。轮次结束时，若其未明置，你摸两张牌。",
  
  ["@@yaolie_target-round"] = "邀烈",
  ["#lvshi__yaolie-choose"] = "邀烈：你可以指定一名未明置身份的角色",
  ["#lvshi__yaolie-duel"] = "邀烈：选择 %dest 使用【决斗】的目标",
  ["$lvshi__yaolie1"] = "英雄当邀，烈志难挡！",
  ["$lvshi__yaolie2"] = "邀天下英雄，共赴此烈局！",
}

local ls = require "packages/lvshi/utility"

-- 轮次开始时选择目标
yaolie:addEffect(fk.RoundStart, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(yaolie.name)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local candidates = table.filter(room:getAlivePlayers(), function(p) return not p.role_shown end)
    if #candidates > 0 then
      local tos = room:askToChoosePlayers(player, {
        targets = candidates,
        max_num = 1,
        min_num = 1,
        skill_name = yaolie.name,
        cancelable = true,
        prompt = "#lvshi__yaolie-choose",
      })
      if tos and #tos > 0 then
        event:setCostData(self, {yaolie_target = tos[1]})
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local target_player = event:getCostData(self).yaolie_target
    room:setPlayerMark(player, "yaolie_target-round", target_player.id)
    room:addPlayerMark(target_player, "@@yaolie_target-round")
  end,
})

-- 身份明置时触发决斗效果
yaolie:addEffect(ls.RoleShown, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(yaolie.name) and target.id == player:getMark("yaolie_target-round")
    and target:getMark("@@yaolie_target-round") > 0
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    -- 选择决斗目标
    local duel_card = Fk:cloneCard("duel")
    -- 使用canUseTo进行完整的合法性检测
    local candidates = table.filter(room:getAlivePlayers(), function(p) return p ~= target and target:canUseTo(duel_card, p) end)
    if #candidates > 0 then
      local tos = room:askToChoosePlayers(player, {
        targets = candidates,
        max_num = 1,
        min_num = 1,
        skill_name = yaolie.name,
        cancelable = false,
        prompt = "#lvshi__yaolie-duel::"..target.id,
      })
      if tos and #tos > 0 then
        event:setCostData(self, {duel_target = tos[1]})
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    player.room:useVirtualCard("lvshi__duel", nil, target, event:getCostData(self).duel_target, yaolie.name)
  end,
})

-- 轮次结束时检查未明置身份
yaolie:addEffect(fk.RoundEnd, {
  anim_type = "drawcard",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(yaolie.name, true, true) then
      local room = player.room
      local target_id = player:getMark("yaolie_target-round")
      if target_id ~= 0 then
        local target_player = room:getPlayerById(target_id)
        if target_player and target_player:getMark("@@yaolie_target-round") > 0 and not target_player.role_shown then
          event:setCostData(self, target_player)
          return true
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local target_player = event:getCostData(self)
    target_player:drawCards(2, yaolie.name)
  end,
})

return yaolie